package com.nvelickovic10.snake.beans.food;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.nvelickovic10.snake.main.AppCore;

public class Bonus extends Food {

	private boolean on;
	private long startTime;
	private BonusType type;

	public Bonus(BonusType type) {
		super();
		setType(type);
	}

	@Override
	public void paintBit(Graphics2D g) {
		check();
		if (isOn()) {
			BufferedImage img = null;
			try {
				if (type == BonusType.YELLOW_BONUS)
					img = ImageIO.read(new File("png/yellowBonus.png"));
				else if (type == BonusType.BLUE_BONUS)
					img = ImageIO.read(new File("png/blueBonus.png"));
				else if (type == BonusType.MAGENTA_BONUS)
					img = ImageIO.read(new File("png/magentaBonus.png"));
				else if (type == BonusType.GREEN_BONUS)
					img = ImageIO.read(new File("png/greenBonus.png"));
			} catch (IOException e) {
				e.printStackTrace();
			}
			g.drawImage(img, null, AppCore.getInstance().getFactor()
					* (getX() - 1) + AppCore.getInstance().getLeft() + 1,
					AppCore.getInstance().getFactor() * (getY() - 1)
							+ AppCore.getInstance().getTop() + 1);
		}
	}

	public boolean isOn() {
		return on;
	}

	public void setOn() {
		this.on = true;
		AppCore.getInstance().setBonusPosition(type);
		setStartTime(System.nanoTime());
	}

	public void setOff() {
		this.on = false;
	}

	public long getStartTime() {
		return startTime;
	}

	public void check() {
		if ((System.nanoTime() - getStartTime()) / 1000000000 > 5)
			setOff();
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public BonusType getType() {
		return type;
	}

	public void setType(BonusType type) {
		this.type = type;
	}
}
